const path = require('path');

const fs = require('fs');

const urlModle = require("url");

const readImg = require('./readImage.js')

module.exports = function (req, res) {

    // console.log(req.method);

    // console.log(req.url);

    // var { pathname: url, query } = urlModle.parse(req.url, true);

    // req.query = query;

    var method = req.method.toLowerCase();

    var url = req.url;
    // console.log(method,url);
    if (method == "get" && url.startsWith('/node_modules/')) {

        fs.readFile(path.join(__dirname, req.url), "utf-8", (err, data) => {
            if (err) return res.end('404')
            res.end(data)
        })

    } else if (method == "get" && url.startsWith('/css/')) {

        fs.readFile(path.join(__dirname, req.url), "utf-8", (err, data) => {
            if (err) return res.end('404')
            res.end(data)
        })

    }
    else if (method == "get" && url.startsWith('/images/')) {


        res.writeHead(200, { "Content-Type": "image/jpeg" });
        readImg.readImage(path.join(__dirname, req.url), res);

    }
    else if (method == "get" && url.startsWith('/js/')) {
        res.writeHead(200, { 'content-type': 'text/html' })

        fs.readFile(path.join(__dirname, req.url), "utf-8", (err, data) => {

            if (err) return console.log(err.message);

            res.end(data)
        })

    } else if (method == "get" && url == "/") {
        res.writeHead(200, { 'content-type': 'text/html' })

        fs.readFile(path.join(__dirname, "./view/index.html"), "utf-8", (err, data) => {

            if (err) return console.log(err.message);

            res.end(data)
        })

    } else if (method == "get" && url == "/index") {

        res.writeHead(200, { 'content-type': 'text/html' })

        fs.readFile(path.join(__dirname, "./view/index.html"), "utf-8", (err, data) => {

            if (err) return console.log(err.message);

            res.end(data)
        })

    } else if (method == "get" && url == "/user") {

        res.writeHead(200, { 'content-type': 'text/html' })

        fs.readFile(path.join(__dirname, "./view/user.html"), "utf-8", (err, data) => {

            if (err) return console.log(err.message);

            res.end(data)
        })
    } else if (method == "get" && url == "/supplier") {

        res.writeHead(200, { 'content-type': 'text/html' })

        fs.readFile(path.join(__dirname, "./view/supplier.html"), "utf-8", (err, data) => {

            if (err) return console.log(err.message);

            res.end(data)
        })
    }
}